package com.freight.tws.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.freight.common.core.constant.Constants;
import com.freight.common.core.domain.R;
import com.freight.common.core.web.page.Condition;
import com.freight.common.core.web.page.Query;
import com.freight.common.security.utils.SecurityUtils;
import com.freight.tws.domain.DriverBank;
import com.freight.tws.service.IDriverBankService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 手机端 - 银行卡controller
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping(Constants.APP_URL + "/")
public class AppDriverBankController {

    private final IDriverBankService driverBankService;

    /**
     * 银行卡列表，分页
     * @param
     * @return
     */
    @GetMapping("bank-list")
    public R<IPage<DriverBank>> bankList(Query query) {
        LambdaQueryWrapper<DriverBank> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DriverBank::getDriverId, SecurityUtils.getUserId());
        lqw.orderByDesc(DriverBank::getIsDefault, DriverBank::getCreateTime);
        IPage<DriverBank> list = driverBankService.page(Condition.getPage(query), lqw);
        return R.ok(list);
    }

    /** 新增银行卡 */
    @PostMapping("bank-add")
    public R add(@RequestBody DriverBank driverBank) {
        driverBank.setDriverId(SecurityUtils.getUserId());
        return driverBankService.save(driverBank) ? R.ok() : R.fail();
    }

    /**
     * 设为默认
     */
    @GetMapping("bank-default/{id}")
    public R setDefault(@PathVariable Long id) {
        return driverBankService.setDefault(id) ? R.ok() : R.fail();
    }

    /**
     * 删除银行卡
     */
    @GetMapping("bank-remove/{id}")
    public R remove(@PathVariable Long id) {
        return driverBankService.removeById(id) ? R.ok() : R.fail();
    }

}
